var combinationSum = function(candidates, target) {
  const res = [];
  const path = [];
  const rec = (index, t) => {
    if (t === 0) {
      res.push([...path]);
      return;
    }
    if (t < candidates[index]) {
      return;
    }
    for(let i = index; i < candidates.length; i++) {
      path.push(candidates[i]);
      rec(i, t - candidates[i]);
      path.pop();
    }
  };
  rec(0, target);
  return res;
};
console.log(combinationSum([2, 3, 6, 7], 7))

